package org.rapan.EJB;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.rapan.EmailUtilities;
import org.rapan.Position;
import org.rapan.exceptions.CreateReportException;
import org.rapan.jpa.Company;
import org.rapan.jpa.Consultant;
import org.rapan.jpa.Report;

@Stateless
public class ReportBean implements ReportInt {

	@EJB
	ConsultantInt conInt;

	@PersistenceContext(unitName = "Consult")
	private EntityManager em;

	@Override
	public void createReport(Company company, Consultant consultant,
			Date startDate, Date endDate, short HPD)
			throws CreateReportException {
		boolean createError = false;
		CreateReportException ex = new CreateReportException();
		if (company == null) {
			ex.errorSet.add("The company name is blank.");
			createError = true;
		}
		if (startDate.before(consultant.getStartDate())) {
			ex.errorSet
					.add("Specified starting date is before the employee's starting date.");
			createError = true;
		}
		if ((consultant.getEndDate() != null)
				&& (endDate.after(consultant.getEndDate()))) {
			ex.errorSet
					.add("Specified ending date is after the employee's leaving date.");
			createError = true;
		}
		if (startDate.after(endDate)) {
			ex.errorSet
					.add("Specified starting date is after the ending date.");
			createError = true;
		}
		if ((HPD <= 0) || (HPD > 8)) {
			ex.errorSet.add("The hours per day are above 8 or 0 or lower.");
			createError = true;
		}
		if (createError) {
			EmailUtilities.sendFailedReportEmail(consultant.getId(),
					ex.errorSet);
			throw ex;
		}

		Report r = new Report();
		r.setCompany(company);
		r.setConsultant(consultant);
		r.setStarting_date(startDate);
		r.setEnd_date(endDate);
		r.setHours_per_day(HPD);
		em.persist(r);
		em.flush();
		em.refresh(r);
		System.out.println(r.getId());
		EmailUtilities.sendSuccessfulReportEmail(r.getId());
	}

	@Override
	public Report viewReport(long id) {
		try {
			Report r = em.find(Report.class, id);
			return r;
		} catch (IllegalArgumentException e) {
			return null;
		}
	}

	@Override
	public Set<Report> getAllReportsForConsultant(Consultant consultant) {
		TypedQuery<Report> q = em.createQuery(
				"select r from Report r where r.consultant.id = :id",
				Report.class);
		q.setParameter("id", consultant.getId());
		Set<Report> repSet = new TreeSet<Report>();
		repSet.addAll(q.getResultList());
		return repSet;
	}

	@Override
	public double getSum(Report r) {
		double sum = 0;
		double coefficient = 10;
		int days = 0;
		Consultant c = r.getConsultant();
		if (c.getPosition() == Position.Junior)
			coefficient = 5;
		else if (c.getPosition() == Position.Normal)
			coefficient = 7.5;
		else if (c.getPosition() == Position.Senior)
			coefficient = 10;
		days = (int)( (r.getEnd_date().getTime() - r.getStarting_date().getTime()) / (1000 * 60 * 60 * 24));
		sum = 100 * days * coefficient * r.getHours_per_day();
		return sum;
	}

}
